package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.RecommandCardBean;
import com.joplus.housekeeper.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/26.
 */
public class RecommandCardAdapter extends RecyclerView.Adapter<RecommandCardAdapter.HorizontalItemHolder> {

    private List<RecommandCardBean.DataBean.RecommendListBean> cardList;
    AdapterView.OnItemClickListener OnItemClickListener;
    private static final Boolean isani = false;
    Context context;

    public RecommandCardAdapter(Context context, List<RecommandCardBean.DataBean.RecommendListBean> bean) {
        this.cardList = bean;
        this.context = context;
    }

    public void notifyDataSetChanged(List<RecommandCardBean.DataBean.RecommendListBean> bean) {
        this.cardList = bean;
        notifyDataSetChanged();
    }

    @Override
    public HorizontalItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_recommand_card_horizontal, parent, false);
        return new HorizontalItemHolder(view, this);

    }

    @Override
    public void onBindViewHolder(HorizontalItemHolder holder, int position) {
        RecommandCardBean.DataBean.RecommendListBean bean = cardList.get(position);
        StringUtils.setText(holder.tv_name, bean.getRecommendName());

        holder.tv_introduction.setText(getColon(bean.getRecommendName()));
//        StringUtils.setText(holder.tv_type_name,bean.getRecommendName());

        //推荐标签(1、24小时最热2、最新上线3、猜您喜欢4、会员福利5、无)
        if (!StringUtils.isEmpty(bean.getRecommendLabel())) {
            switch (bean.getRecommendLabel()) {
                case "1":
                    holder.tv_type_name.setImageResource(R.drawable.rcard_hot);
                    break;
                case "2":
                    holder.tv_type_name.setImageResource(R.drawable.rcard_new);
                    break;
                case "3":
                    holder.tv_type_name.setImageResource(R.drawable.rcard_guess_like);
                    break;
                case "4":
                    holder.tv_type_name.setImageResource(R.drawable.rcard_member_onley);
                    break;
                case "5":
                    holder.tv_type_name.setImageResource(0);
                    break;
            }

        }

        holder.iv_img.setTag(bean.getImg() + position);
        ImageLoader.getInstance().displayImage(bean.getImg(), holder.iv_img);


//        if(bean.getImg())

//        if(!(bean.getImg()+position).equals(holder.iv_img.getTag())){
//            holder.iv_img.setTag(bean.getImg()+position);
//            ImageLoader.getInstance().displayImage(bean.getImg(),holder.iv_img);
//        }

        //设置加粗
        holder.tv_name.getPaint().setFakeBoldText(true);

    }

    @Override
    public int getItemCount() {

        if (cardList == null) {
            return 0;
        } else {
            return cardList.size();
        }

    }


    public void setOnItemClickListener2(AdapterView.OnItemClickListener listener) {
        OnItemClickListener = listener;
    }

    private void onItemHolderClick(HorizontalItemHolder holder) {

    }

    public static class HorizontalItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView iv_img;
        TextView tv_name;
        TextView tv_introduction;
        ImageView tv_type_name;
        RecommandCardAdapter adapter;
        RelativeLayout layout;
        private Boolean isClicked = false;//解决多个手指按下的问题


        public HorizontalItemHolder(View itemView, final RecommandCardAdapter adapter) {
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_introduction = (TextView) itemView.findViewById(R.id.tv_introduction);
            tv_type_name = (ImageView) itemView.findViewById(R.id.tv_type_name);
            this.adapter = adapter;
            layout = (RelativeLayout) itemView.findViewById(R.id.ll_parent);
            layout.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {

            adapter.onItemHolderClick(this);


        }
    }

    public SpannableStringBuilder getColon(String s) {

        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
//        stringBuilder.append("“");
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(16, true);
        //stringBuilder.setSpan(absoluteSizeSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        //stringBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        if (!StringUtils.isEmpty(s)) {
            stringBuilder.append(s);
        }

        return stringBuilder;

    }


}
